<?php
namespace Common\Mysql;
class TaskMysql extends CommonMysql {

    public function addTask($data) {
        $title = v($data,'title');
        verify($title,'notEmpty','抓取任务名称不能为空');

        $urls = v($data,'urls');
        verify($urls,'notEmpty','链接地址不能为空');
        
        $tmpList = array_unique(explode("\n",preg_replace("#\s#","\n",$urls)));

        $urlList = array();
        foreach($tmpList as $url) {
            $url = trim($url);
            if(!trim($url)) continue;
            verify($url,'isUrl','非法抓取地址');
            $urlList[] = $url;
        }
        $urls = implode("\n",$urlList);

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        $parseid = v($data,'parseid');
        verify($parseid,'notEmpty','解析器ID不能为空');
        verify($parseid,'isID','非法解析器ID');

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $timespan = v($data,'timespan',1,true);
        verify($timespan,'notEmpty','执行时间间隔不能为空');
        verify($timespan,'isNumber','非法时间间隔');

        $cid = v($data,'cid');
        verify($cid,'notEmpty','分类ID不能为空');
        verify($cid,'isID','非法分类ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $categoryInfo = M('Category')->find($cid);
        verify($categoryInfo,'notEmpty','分类不存在');
        
        if($this->where("`title`='{$title}' AND `cid` = '{$cid}'")->count()) {
            E('抓取任务已经存在');
        }
        if($categoryInfo['sid'] != $sid) {
            E('无权操作');
        }

        $id = $this->addRecord(array(
            'title'     =>  $title,
            'sid'       =>  $categoryInfo['sid'],
            'cid'       =>  $cid,
            'parseid'   =>  $parseid,
            'urls'      =>  $urls,
            'timespan'  =>  $timespan,
            'createtime'=>  NOW_TIME,
            'updatetime'=>  NOW_TIME,
            'status'    =>  $status,
        ),false,'抓取任务添加失败');

        return $id;
    }

    public function updateTask($data) {
        $id = v($data,'id');
        verify($id,'notEmpty','任务ID不能为空');
        verify($id,'isID','非法任务ID');

        $title = v($data,'title');
        verify($title,'notEmpty','抓取任务名称不能为空');

        $urls = v($data,'urls');
        verify($urls,'notEmpty','链接地址不能为空');
        
        $tmpList = explode("\n",preg_replace("#\s#","\n",$urls));

        $urlList = array();
        foreach($tmpList as $url) {
            $url = trim($url);
            $url = trim($url,'/');
            if(!trim($url)) continue;
            verify($url,'isUrl','非法抓取地址');
            $urlList[] = $url;
        }
        $urlList = array_unique($urlList);
        $urls = implode("\n",$urlList);

        $sid = v($data,'sid');
        verify($sid,'notEmpty','网站ID不能为空');
        verify($sid,'isID','非法网站ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $sort = v($data,'sort',1,true);
        verify($sort,'isNumber','非法排序');

        $parseid = v($data,'parseid');
        verify($parseid,'notEmpty','解析器ID不能为空');
        verify($parseid,'isID','非法解析器ID');

        $timespan = v($data,'timespan',1,true);
        verify($timespan,'notEmpty','执行时间间隔不能为空');
        verify($timespan,'isNumber','非法时间间隔');

        $cid = v($data,'cid');
        verify($cid,'notEmpty','分类ID不能为空');
        verify($cid,'isID','非法分类ID');

        $status = v($data,'status',1,true);
        verify($status,'isStatus','非法状态');

        $categoryInfo = M('Category')->find($cid);
        verify($categoryInfo,'notEmpty','分类不存在');
        
        if($this->where("`title`='{$title}' AND `cid` = '{$cid}' AND `id` <> {$id}")->count()) {
            E('抓取任务已经存在');
        }

        $this->updateRecord(array(
            'id'        =>  $id,
            'title'     =>  $title,
            'parseid'   =>  $parseid,
            'urls'      =>  $urls,
            'timespan'  =>  $timespan,
            'updatetime'=>  NOW_TIME,
            'status'    =>  $status,
        ),false,'抓取任务添加失败');

        return $id;
    }
}
?>
